﻿using CommunityToolkit.Mvvm.ComponentModel;
using MeasureAbstract.Base;
using MeasureAbstract.IMotionDevice;
using MeasureAbstract.IVectorNetworkAnalyzer;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace MeasureAbstract.ISignalGenerator
{
    [INotifyPropertyChanged]
    public abstract partial class ISignalGenerator : IDeviceBase
    {
        #region 父类

        public abstract string Name { get; }
        public abstract string Vender { get; }
        public abstract string Model { get; }
        public abstract string Description { get; }
        public bool _RadioFrequencyState = false;
        public bool RadioFrequencyState
        {
            get
            {
                return _RadioFrequencyState;
            }
            set
            {
                SetProperty(ref _RadioFrequencyState, value);
            }
        }


        public bool _ConnectState = false;
        public bool ConnectState
        {
            get
            {
                return _ConnectState;
            }
            set
            {
                SetProperty(ref _ConnectState, value);
            }
        }

        public bool _MalfunctionState = false;
        public bool MalfunctionState
        {
            get
            {
                return _MalfunctionState;
            }
            set
            {
                SetProperty(ref _MalfunctionState, value);
            }
        }

        public abstract void Connect();
        public abstract void DisConnect();
        public abstract void ReSet();

        #endregion


        #region 参数

        /// <summary>
        /// 起止频率模式
        /// </summary>
        [ObservableProperty]
        private bool seMode = true;

        /// <summary>
        /// 中频跨度模式
        /// </summary>
        [ObservableProperty]
        private bool cbMode = false;

        /// <summary>
        /// 列表模式
        /// </summary>
        [ObservableProperty]
        private bool listMode = false;

        /// <summary>
        /// 设备参数
        /// </summary>
        public SignalGeneratorConfig Config { get; set; }


        /// <summary>
        /// 起始频率
        /// </summary>
        [ObservableProperty]
        private double startFrequency = 100;
        /// <summary>
        /// 起始频率单位
        /// </summary>
        [ObservableProperty]
        private string startFrequencyUnit = "MHz";

        /// <summary>
        /// 结束频率
        /// </summary>
        [ObservableProperty]
        private double endFrequency = 200;
        /// <summary>
        /// 结束频率单位
        /// </summary>
        [ObservableProperty]
        private string endFrequencyUnit = "MHz";

        /// <summary>
        /// 频率单位
        /// </summary>
        public abstract ObservableCollection<string> Unit { get; }

        /// <summary>
        /// 频点数量
        /// </summary>
        [ObservableProperty]
        private int frequencyCount = 21;

        /// <summary>
        /// 中心频率
        /// </summary>
        [ObservableProperty]
        private double centerFrequency = 200;
        /// <summary>
        /// 中心频率单位
        /// </summary>
        [ObservableProperty]
        private string centerFrequencyUnit = "MHz";

        /// <summary>
        /// 频率跨度
        /// </summary>
        [ObservableProperty]
        private double bandFrequency = 200;
        /// <summary>
        /// 频率跨度单位
        /// </summary>
        [ObservableProperty]
        private string bandFrequencyUnit = "MHz";


        /// <summary>
        /// 频率列表
        /// </summary>
        [ObservableProperty]
        private string frequencys = "100,200,300";

        /// <summary>
        /// 中频带宽
        /// </summary>
        [ObservableProperty]
        private int iFBW = 10;

        /// <summary>
        /// 中频带宽单位
        /// </summary>
        [ObservableProperty]
        private string iFBWUnit = "KHz";

        /// <summary>
        /// 使用或不使用射频功能
        /// </summary>
        [ObservableProperty]
        private bool radioEnable = true;

        /// <summary>
        /// 发射功率
        /// </summary>
        [ObservableProperty]
        private double outputPower_dBm = -20;

        /// <summary>
        /// 使用设备间联动
        /// </summary>
        [ObservableProperty]
        public bool deviceInteractEnable = false;

        /// <summary>
        /// 使用触发输入
        /// </summary>
        [ObservableProperty]
        public bool triggerInEnable = true;

        /// <summary>
        /// 选择的触发输入口
        /// </summary>
        public abstract string SelectedTriggerInPort { get; set; }

        /// <summary>
        /// 设备的触发输入接口列表
        /// </summary>
        public abstract ObservableCollection<string> TriggerInPorts { get; }

        /// <summary>
        /// 选择的触发输入信号
        /// </summary>
        public abstract string SelectedTriggerInSignal { get; set; }

        /// <summary>
        /// 设备的触发输入信号列表
        /// </summary>
        public abstract ObservableCollection<string> TriggerInSignals { get; }

        /// <summary>
        /// 选择的触发输入信号脉冲宽度
        /// </summary>
        public abstract string SelectedTriggerInSignalWidth { get; set; }


        /// <summary>
        /// 选择的触发输入相应动作
        /// </summary>
        public abstract string SelectedTriggerInAction { get; set; }

        /// <summary>
        /// 设备的触发输入动作列表
        /// </summary>
        public abstract ObservableCollection<string> TriggerInActions { get; }

        /// <summary>
        /// 使用触发输出
        /// </summary>
        [ObservableProperty]
        public bool triggerOutEnable = true;

        /// <summary>
        /// 选择的触发输出口
        /// </summary>
        public abstract string SelectedTriggerOutPort { get; set; }

        /// <summary>
        /// 设备的触发输出接口列表
        /// </summary>
        public abstract ObservableCollection<string> TriggerOutPorts { get; }

        /// <summary>
        /// 选择的触发输出信号
        /// </summary>
        public abstract string SelectedTriggerOutSignal { get; set; }

        /// <summary>
        /// 选择的触发输出信号脉冲宽度
        /// </summary>
        public abstract string SelectedTriggerOutSignalWidth { get; set; }

        /// <summary>
        /// 设备的触发输出信号列表
        /// </summary>
        public abstract ObservableCollection<string> TriggerOutSignals { get; }

        /// <summary>
        /// 选择的触发输出动作时机
        /// </summary>
        public abstract string SelectedTriggerOutTime { get; set; }

        /// <summary>
        /// 设备的触发输出动作时机列表
        /// </summary>
        public abstract ObservableCollection<string> TriggerOutTimes { get; }

        #endregion


        #region 行为

        /// <summary>
        /// 开启关闭射频
        /// </summary>
        /// <param name="Open">true开启射频，false关闭射频</param>
        public abstract void OutPut(bool Open);


        /// <summary>
        /// 设置起始频率
        /// </summary>
        /// <param name="ChannelNumber">通道号</param>
        /// <param name="MeasurementName">测量名称</param>
        /// <param name="StartFrequency">起始频率</param>
        /// <param name="Unit">单位</param>
        /// <returns></returns>
        public abstract int SetStartFrequency(long StartFrequency, string Unit = "Hz");

        /// <summary>
        /// 设置结束频率
        /// </summary>
        /// <param name="ChannelNumber">通道号</param>
        /// <param name="MeasurementName">测量名称</param>
        /// <param name="EndFrequency">结束频率</param>
        /// <param name="Unit">单位</param>
        /// <returns></returns>
        public abstract int SetEndFrequency(long EndFrequency, string Unit = "Hz");


        /// <summary>
        /// 设置扫描点数
        /// </summary>
        /// <param name="ChannelNumber">通道号</param>
        /// <param name="MeasurementName">测量名称</param>
        /// <param name="PointCount">测量点数</param>
        /// <returns></returns>
        public abstract int SetScanPoints(int PointCount);

        /// <summary>
        /// 设置发射功率
        /// </summary>
        /// <param name="ChannelNumber">通道号</param>
        /// <param name="MeasurementName">测量名称</param>
        /// <param name="OutputPower_dBm">发射功率</param>
        /// <returns></returns>
        public abstract int SetOutputPower(double OutputPower_dBm = -20);

        /// <summary>
        /// 设置中频带宽
        /// </summary>
        /// <param name="ChannelNumber">通道号</param>
        /// <param name="Bandwidth">带宽</param>
        /// <param name="Unit">单位</param>
        /// <returns></returns>
        public abstract int SetIntermediateFrequencyBandwidth(long Bandwidth, string Unit = "Hz");

        /// <summary>
        /// 设置触发源
        /// </summary>
        /// <param name="TriggerSource">内部INT，外部EXT，手动MAN，总线BUS</param>
        /// <returns></returns>
        public abstract int SetTriggerSource(TriggerSource TriggerSource);

        /// <summary>
        /// 设置触发输入
        /// </summary>
        /// <param name="Interface">哪个口作为触发输入的接口</param>
        /// <param name="TriggerSignalType">触发信号的类型</param>
        /// <param name="Delay_ms">收到触发信号后，延时一定时间进行进行动作</param>
        /// <returns></returns>
        public abstract int SetTriggerInput(string Interface, TriggerSignalType TriggerSignalType,double Width, double Delay_ms = 0);

        /// <summary>
        /// 设置触发输出
        /// </summary>
        /// <param name="Enable">使能</param>
        /// <param name="Interface">哪个接口作为触发输出</param>
        /// <param name="ChannelNumber">哪个通道</param>
        /// <param name="TriggerSignalType">触发信号类型</param>
        /// <param name="Width">信号宽度</param>
        /// <returns></returns>
        public abstract int SetTriggerOut(bool Enable, string Interface, int ChannelNumber,string time, TriggerSignalType TriggerSignalType, double Width);

        /// <summary>
        /// 设置扫描模式
        /// </summary>
        /// <param name="SweepMode">连续还是保持</param>
        /// <returns></returns>
        public abstract int SetSweepMode(SweepMode SweepMode = SweepMode.连续);

        /// <summary>
        /// 触发一次的扫描范围
        /// </summary>
        /// <param name="SweepScope">整个通道的所有测量;同一端口的测量;一个点</param>
        /// <returns></returns>
        public abstract int SetSweepScope(SweepScope SweepScope = SweepScope.通道);

        /// <summary>
        /// 获取错误
        /// </summary>
        /// <param name="ErrorInfo"></param>
        /// <returns></returns>
        public abstract int GetError(ref string ErrorInfo);


        public long GetHZ(double Value, string Unit)
        {
            long NewValue = 0;
            switch (Unit.ToUpper())
            {
                case "HZ":
                    NewValue = (long)Value;
                    break;
                case "KHZ":
                    NewValue = (long)(Value * 1000);
                    break;
                case "MHZ":
                    NewValue = (long)(Value * 1000000);
                    break;
                case "GHZ":
                    NewValue = (long)(Value * 1000000000);
                    break;
            }
            return NewValue;
        }


        ////频率扫描 设置频点
        //public abstract int FrequencySweepSetFrequency_SEMode(long StartFrequency_HZ,long EndFrequency_HZ,int Count);
        //public abstract int FrequencySweepSetFrequency_CBMode(long CenterFrequency_HZ,long BandWidth_HZ,int Count);
        //public abstract int FrequencySweepSetFrequency_ListMode(List<long> Points);


        //public abstract int FrequencySweepSetSweepParameters(string Direction="正向",double DwellTime=10,string Source="外部");


        #endregion
        /// <summary>
        /// 等待矢网操作完成
        /// </summary>
        /// <param name="PollingTime_ms">轮询时间每隔这么长时间询问一次矢网</param>
        /// <returns></returns>
        public abstract int Wait(int PollingTime_ms);
    }
    public sealed partial class SignalGeneratorConfig : CommunicationConfig
    {

    }

    


}
